#!/usr/bin/env bash

BASEDIR=$(dirname $(readlink -f $0))

echo "basedir --> $BASEDIR"

DIST=$BASEDIR/dist
if [ "$1" = "clean" ]; then
  ./mvnw clean
  rm -rf $DIST
  exit 0
fi
PROJECT=dae
SPEC=$PROJECT.spec
VERSION=$(grep "^Version\:" $SPEC|awk '{print $2}')
SOURCE=$PROJECT-$VERSION.tar.gz

if [ ! -f $DIST/SOURCES/$SOURCE ]; then
  [ ! -d $DIST/SOURCES ] && mkdir -p $DIST/SOURCES
  tar -zcvf $DIST/SOURCES/$SOURCE ./src ./pom.xml ./dae.service ./mvnw ./.mvn --transform="s#\\.#$PROJECT-$VERSION#"
fi

mkdir -p $DIST/{BUILD,BUILDROOT,RPMS}
rpmbuild --verbose \
  --define "_topdir $DIST" \
  --define "_rpmdir $DIST/RPMS" \
  -bb "$SPEC"








